package com.example.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Exhibition;
import com.example.springboot.service.ExhibitionService;

@CrossOrigin
@RestController

@RequestMapping("/exhibition")
public class ExhibitionController {
    @Autowired
    ExhibitionService exhibitionService;


    @PostMapping("/add")
    public Result add(@RequestBody Exhibition exhibition){
        try{
            exhibitionService.save(exhibition);
        }catch(Exception e){
            return Result.error("插入数据错误");
        }
        return Result.success();
    }


    @PutMapping("/update")
    public Result update(@RequestBody Exhibition exhibition){
        try{
            exhibitionService.updateById(exhibition);
        }catch(Exception e){
            return Result.error("更新数据错误");
        }
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try{
            exhibitionService.removeById(id);
        }catch(Exception e){
            return Result.error("删除数据错误");
        }
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Exhibition> exhibitionList=exhibitionService.list();
        return Result.success(exhibitionList);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Exhibition exhibition=exhibitionService.getById(id);
        return Result.success(exhibition);
    }

    @GetMapping("/selectByPlate/{plate}")
    public Result selectByPlate(@PathVariable String plate){
        List<Exhibition> exhibitionList1=exhibitionService.selectByPlate(plate);
        return Result.success(exhibitionList1);
    }
}
